#bash

export tools_path=/mnt/ilustre/app/medical/tools
export tools=$tools_path
export data_path=$tools_path/../.data
export data=$data_path
export vcf_path=$data_path/vcf/gatk/
export dest=$tools_path/../.pub
export pub=$dest
export home=$HOME
# pid

pid=$$
cmd_name="`basename $0` $@"


export pid_log=.pid.log
if [ ! -e "$pid_log" ]; then
	:> $pid_log
fi

echo $$ `id -u` `date +%m%d%H%M` `hostname` $$ $cmd_name >>$pid_log

cmd_done=$tools_path/script/log_done.sh

#OPTS

export ii=577
export gg=598

if test ! $ii -eq $(id -u); then
	if test $gg -eq $(id -g); then
		umask 0007
		# export umask
	else
		umask 0000
	fi
else
umask 0007
# export umask
fi

export ss=5
# if test $ii -eq $(id -u); then
# elif test $gg -eq $(id -g); then
# umask 0007
# export umask
# else
# umask 0000
# export umask
# fi



# echo script name
# if test -z "$log"; then
# export log=.log
# else
# export log
# fi

export log=${log:=.log}

# if [ ! -e "$log" ]; then
	# :> $log
# fi

exec 6>&1
exec 1>>$log
exec 2>&1


# log_cmd=.log.$cmd_name
# if [ ! -e "$log" ]; then
	# :> $log
# fi

# start path
path_start=$(pwd)


# if [ -z $out_prefix ]; then out_prefix=1; fi

out_prefix=${out_prefix:=1}
sample_name=${sample_name:=sample1}

# if [ -z $sample_name ]; then sample_name=test;  fi

sample_name=`echo $sample_name|tr A-Z a-z`

echo; echo "##################################################################################################################################################################"
echo `id -u;date;pwd;hostname`
echo;echo `basename $0` $@

echo; echo ------------------------------------------
echo out prefix: $out_prefix
echo sample_name: $sample_name
# path



PATH=:.:/mnt/ilustre/app/medical/tools/tools:/mnt/ilustre/app/medical/tools/script:/mnt/ilustre/app/medical/tools/py:/mnt/ilustre/app/medical/tools/script/bt_nprot_0307:/mnt/ilustre/app/medical/tools/annovar/bin:/mnt/ilustre/app/medical/tools/bcftools-1.2:/mnt/ilustre/app/medical/tools/bedtools2/bin:/mnt/ilustre/app/medical/tools/blast-2.2.25+/bin:/mnt/ilustre/app/medical/tools/blat:/mnt/ilustre/app/medical/tools/bowtie-0.12.8:/mnt/ilustre/app/medical/tools/bowtie2-2.2.5:/mnt/ilustre/app/medical/tools/bwa-0.7.12:/mnt/ilustre/app/medical/tools/CNVnator-0.3.2:/mnt/ilustre/app/medical/tools/cnv-seq:/mnt/ilustre/app/medical/tools/cpdf-source-2.1.1:/mnt/ilustre/app/medical/tools/cutadapt-1.8.1/bin:/mnt/ilustre/app/medical/tools/ensembl-tools-release-82/scripts/variant_effect_predictor:/mnt/ilustre/app/medical/tools/fastx_toolkit-0.0.13.2/bin:/mnt/ilustre/app/medical/tools/GEM-binaries-Linux-x86_64-core_i3-20130406-045632/bin:/mnt/ilustre/app/medical/tools/HMMcopy/bin:/mnt/ilustre/app/medical/tools/HMMcopy/util:/mnt/ilustre/app/medical/tools/htslib-1.2.1:/mnt/ilustre/app/medical/tools/jdk1.7.0_75/bin:/mnt/ilustre/app/medical/tools/jre1.8.0_40/bin:/mnt/ilustre/app/medical/tools/ocaml-4.02.3/bin:/mnt/ilustre/app/medical/tools/polyphen-2.2.2/bin:/mnt/ilustre/app/medical/tools/Python-2.7.10:/mnt/ilustre/app/medical/tools/root-6.04.10/bin:/mnt/ilustre/app/medical/tools/samtools-1.2:/mnt/ilustre/app/medical/tools/scala-2.11.8/bin:/mnt/ilustre/app/medical/tools/seqtk-master:/mnt/ilustre/app/medical/tools/sift4.0.3b/bin:/mnt/ilustre/app/medical/tools/vcftools_0.1.12b/bin:/mnt/ilustre/app/medical/tools/vcftools_0.1.12b/perl:/mnt/ilustre/app/medical/Tools:::/bin:/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/usr/java/latest/bin:/usr/lib64/qt-3.3/bin:::/mnt/ilustre/app/pub/bin:/mnt/ilustre/app/pub/blast+/ncbi-blast-2.2.31+/bin:/mnt/ilustre/app/pub/emboss-6.5.7/bin:/mnt/ilustre/app/pub/gcc/5.1.0/bin:/mnt/ilustre/app/pub/gcc5/bin:/mnt/ilustre/app/pub/gcc/gcc-4.8.4/bin:/mnt/ilustre/app/pub/java/8u45/bin:/mnt/ilustre/app/pub/perl/5.10.1/bin:/mnt/ilustre/app/pub/perl/5.10.1/bin:/mnt/ilustre/app/pub/python/2.7.10/bin:/mnt/ilustre/app/pub/R/bin:/mnt/ilustre/app/rna/assemble_mrna/trinityrnaseq_r20140413:/mnt/ilustre/app/rna/bin:/mnt/ilustre/app/rna/function_enrichment/goatools-master/scripts:/mnt/ilustre/app/rna/miRNA/mirdeep2:/mnt/ilustre/app/rna/miRNA/miRNA_pipeline:/mnt/ilustre/app/rna/miRNA/miRNA_pipeline/bin:/mnt/ilustre/app/rna/public_scripts:/mnt/ilustre/app/meta/bcl2fastq/bcl2fastq2-v2.16/bin:/mnt/ilustre/app/meta/Softwares/gnuplot/gnuplot/bin:/mnt/ilustre/app/zhuohao/software/kobas2.0/20150126/scripts:/mnt/ilustre/users/bingxu.liu/workspace/RNA_Pipeline:/opt/ganglia/bin:/opt/ganglia/sbin:/opt/ibutils/bin:/opt/maui/bin:/opt/openmpi/bin:/opt/pdsh/bin:/opt/rocks/bin:/opt/rocks/sbin:/opt/torque/bin:/opt/torque/sbin:.:/mnt/ilustre/app/medical/tools/tools:/mnt/ilustre/app/medical/tools/script:/mnt/ilustre/app/medical/tools/py:/mnt/ilustre/app/medical/tools/script/bt_nprot_0307:/mnt/ilustre/app/medical/tools/annovar/bin:/mnt/ilustre/app/medical/tools/bcftools-1.2:/mnt/ilustre/app/medical/tools/bedtools2/bin:/mnt/ilustre/app/medical/tools/blast-2.2.25+/bin:/mnt/ilustre/app/medical/tools/blat:/mnt/ilustre/app/medical/tools/bowtie-0.12.8:/mnt/ilustre/app/medical/tools/bowtie2-2.2.5:/mnt/ilustre/app/medical/tools/bwa-0.7.12:/mnt/ilustre/app/medical/tools/CNVnator-0.3.2:/mnt/ilustre/app/medical/tools/cnv-seq:/mnt/ilustre/app/medical/tools/cpdf-source-2.1.1:/mnt/ilustre/app/medical/tools/cutadapt-1.8.1/bin:/mnt/ilustre/app/medical/tools/ensembl-tools-release-82/scripts/variant_effect_predictor:/mnt/ilustre/app/medical/tools/fastx_toolkit-0.0.13.2/bin:/mnt/ilustre/app/medical/tools/GEM-binaries-Linux-x86_64-core_i3-20130406-045632/bin:/mnt/ilustre/app/medical/tools/HMMcopy/bin:/mnt/ilustre/app/medical/tools/HMMcopy/util:/mnt/ilustre/app/medical/tools/htslib-1.2.1:/mnt/ilustre/app/medical/tools/jdk1.7.0_75/bin:/mnt/ilustre/app/medical/tools/jre1.8.0_40/bin:/mnt/ilustre/app/medical/tools/ocaml-4.02.3/bin:/mnt/ilustre/app/medical/tools/polyphen-2.2.2/bin:/mnt/ilustre/app/medical/tools/Python-2.7.10:/mnt/ilustre/app/medical/tools/root-6.04.10/bin:/mnt/ilustre/app/medical/tools/samtools-1.2:/mnt/ilustre/app/medical/tools/scala-2.11.8/bin:/mnt/ilustre/app/medical/tools/seqtk-master:/mnt/ilustre/app/medical/tools/sift4.0.3b/bin:/mnt/ilustre/app/medical/tools/vcftools_0.1.12b/bin:/mnt/ilustre/app/medical/tools/vcftools_0.1.12b/perl:/mnt/ilustre/app/medical/Tools:::/bin:/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/usr/java/latest/bin:/usr/lib64/qt-3.3/bin:::/mnt/ilustre/app/pub/bin:/mnt/ilustre/app/pub/blast+/ncbi-blast-2.2.31+/bin:/mnt/ilustre/app/pub/emboss-6.5.7/bin:/mnt/ilustre/app/pub/gcc/5.1.0/bin:/mnt/ilustre/app/pub/gcc5/bin:/mnt/ilustre/app/pub/gcc/gcc-4.8.4/bin:/mnt/ilustre/app/pub/java/8u45/bin:/mnt/ilustre/app/pub/perl/5.10.1/bin:/mnt/ilustre/app/pub/perl/5.10.1/bin:/mnt/ilustre/app/pub/python/2.7.10/bin:/mnt/ilustre/app/pub/R/bin:/mnt/ilustre/app/rna/assemble_mrna/trinityrnaseq_r20140413:/mnt/ilustre/app/rna/bin:/mnt/ilustre/app/rna/function_enrichment/goatools-master/scripts:/mnt/ilustre/app/rna/miRNA/mirdeep2:/mnt/ilustre/app/rna/miRNA/miRNA_pipeline:/mnt/ilustre/app/rna/miRNA/miRNA_pipeline/bin:/mnt/ilustre/app/rna/public_scripts:/mnt/ilustre/app/meta/bcl2fastq/bcl2fastq2-v2.16/bin:/mnt/ilustre/app/meta/Softwares/gnuplot/gnuplot/bin:/mnt/ilustre/app/zhuohao/software/kobas2.0/20150126/scripts:/mnt/ilustre/users/bingxu.liu/workspace/RNA_Pipeline:/opt/ganglia/bin:/opt/ganglia/sbin:/opt/ibutils/bin:/opt/maui/bin:/opt/openmpi/bin:/opt/pdsh/bin:/opt/rocks/bin:/opt/rocks/sbin:/opt/torque/bin:/opt/torque/sbin:/mnt/ilustre/users//fei.yang/bin:/mnt/ilustre/app/pub/bin:/mnt/ilustre/app/pub/lib:/mnt/ilustre/app/rna/lib:/mnt/ilustre/app/pub/python/2.7.10/bin:/mnt/ilustre/app/rna/miRNA/miRNA_pipeline:/mnt/ilustre/app/rna/miRNA/mirdeep2:/mnt/ilustre/app/zhuohao/software/kobas2.0/20150126/scripts:/mnt/ilustre/app/rna/function_enrichment/goatools-master/scripts:/mnt/ilustre/app/pub/gcc/5.1.0/bin/:/mnt/ilustre/app/pub/perl/5.10.1/bin:/mnt/ilustre/app/pub/python/2.7.10/bin/:/mnt/ilustre/app/pub/R/bin/:/mnt/ilustre/app/meta/Softwares/gnuplot/gnuplot/bin/:/mnt/ilustre/app/medical/tools/R//mnt/ilustre/app/rna/miRNA/miRNA_pipeline/bin/:/mnt/ilustre/users/fei.yang/perl5/bin:/mnt/ilustre/app/medical/tools/seqtk-master:/mnt/ilustre/app/medical/tools/HMMcopy/bin:/mnt/ilustre/app/medical/tools/HMMcopy/util:/mnt/ilustre/app/medical/tools/script:/opt/openmpi/bin:/usr/lib64/qt-3.3/bin:/mnt/ilustre/app/pub/python-2.7/bin:/mnt/ilustre/app/pub/gcc/gcc-4.8.4/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/ganglia/bin:/opt/ganglia/sbin:/opt/ibutils/bin:/usr/java/latest/bin:/opt/maui/bin:/opt/torque/bin:/opt/torque/sbin:/opt/pdsh/bin:/opt/rocks/bin:/opt/rocks/sbin:/mnt/ilustre/app/medical/tools/htslib-1.2.1:/mnt/ilustre/users/bingxu.liu/workspace/RNA_Pipeline/:/mnt/ilustre/app/meta/Softwares/gnuplot/gnuplot/bin/:/mnt/ilustre/app/rna/bin:/mnt/ilustre/app/pub/blast+/ncbi-blast-2.2.31+/bin:/mnt/ilustre/app/rna/public_scripts

PATH=${tools_path}/fastq-tools-0.8/bin:$PATH
PATH=${tools_path}/HMMcopy/bin:${tools_path}/HMMcopy/util:$PATH
PATH=$tools_path/Python-2.7.10:${tools_path}/seqtk-master:$tools_path/mutt-1.6.1/bin:$PATH
PATH=${tools_path}/script:${tools_path}/scripts:${tools_path}:${tools_path}/htslib-1.2.1:$PATH
PATH=$tools_path/tools:$PATH:$tools_path/script/SNPP:/mnt/ilustre/app/rna/bin/
# /mnt/ilustre/app/rna/bin/ for matlab

export PATH

# . $home/.bash_profile

# dest_home

dest_home='http://192.168.10.113:8000'
http='http://192.168.10.113:8000'



# lib

export  LD_LIBRARY_PATH=${tools_path}/htslib-1.2.1:${tools_path}/zlib-1.2.8:$LD_LIBRARY_PATH:${tools_path}/yeppp-1.0.0/binaries/linux/x86_64

#include

export C_INCLUDE_PATH=$C_INCLUDE_PATH:/mnt/ilustre/app/medical/tools/root-6.04.10/include
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/mnt/ilustre/app/medical/tools/root-6.04.10/include


# qsub
if test -z "$cpu_num" -a -z "$cpu"; then # for backward compatibility # if each of them is null; then set
	num=$(cat /proc/sys/kernel/random/uuid | cksum | awk -F ' ' '{print $1}')
	tf=`echo $num %2|bc`
    num=
	if test "$tf" == "0"; then
	cpu_num=9
	cpu=9
	else
	cpu_num=8
	cpu=8
	fi
fi


queue=${queue:=md}

# if test -n "$mem"; then
# mem=`basename $mem gb`
# else
# mem=12g
# fi




# R
export R=/mnt/ilustre/app/pub/R/bin/R

export R_LIBS=${tools_path}/R:$R_LIBS #?




# matlab
export matlab=/mnt/ilustre/app/rna/bin/matlab



# perl
perl_base=${tools_path}/perl_module/
perl_base1=$perl_base/lib/perl5
perl_base11=$perl_base1/x86_64-linux-thread-multi
perl_base12=$perl_base1/x86_64-linux
PERL5LIB=$perl_base:$perl_base11:$perl_base12:$PERL5LIB
PERL5LIB=$PERL5LIB:${tools_path}/perl_module/lib/perl5/x86_64-linux
PERL5LIB=$PERL5LIB:${tools_path}/vcftools_0.1.12b/perl/ #?
PERL5LIB=$PERL5LIB:${tools_path}/perl_module/Spreadsheet-WriteExcel-2.40/lib
PERL5LIB=$PERL5LIB:${tools_path}/perl_module/Env-Path-0.19/blib/lib
PERL5LIB=$PERL5LIB:${tools_path}/perl_module/File-chdir-0.1010/blib/lib
PERL5LIB=$PERL5LIB:${tools_path}/perl_module/Getopt-Long-Descriptive-0.099/blib/lib
PERL5LIB=$PERL5LIB:${tools_path}/quandico-1.13/QUANDICO-v1.12/blib/lib


pm_path=${tools_path}/pm
PERL5LIB=$PERL5LIB:$pm_path/lib/perl5/x86_64-linux-thread-multi:$pm_path/lib/perl5

pm_home=/mnt/ilustre/app/pub/perl/5.10.1/lib
pm_home1=$pm_home/5.10.1:$pm_home/5.10.1/x86_64-linux-thread-multi
pm_home2=$pm_home/site_perl/5.10.1:$pm_home/site_perl/5.10.1/x86_64-linux-thread-multi

PERL5LIB=$PERL5LIB:$pm_home1:$pm_home2

export PERL5LIB

# java


# if test $mem -gt 20; then
# mem0=18
# el

# if test $mem -gt 15; then # unset $mem0, 12/05/2016
# mem0=`echo $mem-2|bc`
# elif test $mem -gt 2; then
# mem0=`echo $mem-1|bc`
# else
# mem0=1
# fi

if [ -z "$mem" ]; then
	mem=20g
elif echo $mem |grep -P '^[1-9][0-9]*$' >/dev/null; then
	mem=${mem}g
elif echo $mem |grep -P '^[1-9][0-9]*g$'>/dev/null; then
	mem=$mem
else
	echo memery not support $mem; exit 65;
fi

mem0=$mem



# if test -e $tmp; then echo rm $tmp; rm -r $tmp; fi # if another tools in using tmp, this goes bad.

export tmp="-Djava.io.tmpdir=./.tmp"
export java_memory="${mem0} -Xms${mem0} $tmp"
export j_mem="-Xmx12g $tmp"
export java_mem="-Xmx${mem0} $tmp"


java_run7="java7 $java_mem -jar $tools_path/jars"
java_run6="java6 $java_mem -jar $tools_path/jars"
java_run8="java8 $java_mem -jar $tools_path/jars"
java_run="java $java_mem -jar $tools_path/jars"

echo java_memory $java_memory
echo j_mem $j_mem
echo java_mem $java_mem

export CLASSPATH=.:${tools_path}/java:${tools_path}/jexcelapi/jxl.jar




# cutadaptor or mark primer

# export cut_primer_len5=21
# export cut_primer_len3=25


# bwa
export num_thread=10

read_group=@RG\\tID:${sample_name}\\tPL:ILLUMINA\\tSM:$sample_name


# samtools
export threads_sam=6
if test -z $threads; then threads=$threads_sam; fi
# bedtools
# vcftools


# gatk nt and nct 1/2
if test -z "$data_thread_num"; then export data_thread_num=4; fi
if test -z "$cpu_thread_num"; then export cpu_thread_num=4; fi

nt=4
nct=4
if test -z "$interval"; then export interval=$data_path/intervals/1/b37.0.bed; fi
if test -z "$intervals"; then export intervals=$data_path/intervals/1/b37.0.bed; fi

# snpeff snpsift
export snpeff_db_version=GRCh37.75


# common
# data


# export genome_name=b37.fa
# export genome_brca12_name=brca12.b37.fasta
export ref_genome_version=b37
export genome_assembly=b37
export dbsnp_version=138
# export ref_genome=${data_path}/ref/b37/$genome_name
# export ref_genome=${data_path}/ref/b37/brca12/$genome_brca12_name
# echo $interval
# echo $ref_genome
. ${tools_path}/script/ref_genome
# . .ref_genome
# rm .ref_genome
export dbsnp=${data_path}/dbsnp/dbsnp.$ref_genome_version.vcf.gz
export cosmic=${data_path}/cosmic/cosmic.$ref_genome_version.vcf.gz
export hapmap=${data_path}/hapmap/hapmap.$ref_genome_version.vcf.gz


export interval_brca12=${data_path}/intervals/1/brca.qiagen.NGHS-102X-Covered.b37.bed
export interval_brca44=${data_path}/intervals/1/brca.qiagen.NGHS-001X-Covered.b37.bed
export interval_luca50ph=${data_path}/intervals/1/lunca50.b37.sort.bed
export interval_brca12ph=${data_path}/intervals/1/brca12_b37_sort_ph.bed


# tools

###--------------- tools path ---------------###

export script_path=${tools_path}/script/
export cutadapt=${tools_path}/cutadapt-1.8.1/bin/cutadapt
export bwa=${tools_path}/bwa-0.7.12/bwa
export samtools=${tools_path}/samtools-1.2/samtools
export picard_path=${tools_path}/picard-tools-1.119/
export picard_path0=${tools_path}/picard-tools-1.119
export gatk_path=${tools_path}/GenomeAnalysisTK-1.6-9-g47df7bb/
export gatk3_path=${tools_path}
export gatk=${gatk3_path}/GenomeAnalysisTK-3.3-0/GenomeAnalysisTK.jar
export snpeff_path=${tools_path}/snpEff
export snpeff=${snpeff_path}/snpEff.jar
export snpsift=${snpeff_path}/SnpSift.jar
export bcftools=${tools_path}/bcftools-1.2/bcftools
export bgzip=${tools_path}/htslib-1.2.1/bgzip
export tabix=${tools_path}/htslib-1.2.1/tabix

export quandico=${tools_path}/quandico-1.13/QUANDICO-v1.12/scripts/quandico
export qgetcounts=${tools_path}/quandico-1.13/QUANDICO-v1.12/scripts/qgetcounts
export qcluster=${tools_path}/quandico-1.13/QUANDICO-v1.12/scripts/qcluster
# export fastuniq=${tools_path}/FastUniq/fastuniq
###--------------- end tools path ---------------###



# makedir

if test "$makedir" = "true" -o "$makedir" = "T"; then

	for i in $#; do
		if test -e $i; then
			if
				echo $i |perl -n -e 'if(/^\//){exit 0;}else{exit 2;}'; then
				echo $i
			else
				echo ../$i
			fi
		fi
	done


	datee=`date +h%Hm%Ms%S_m%md%d_%y`
	dir_name=${sample_name}_$datee
	if test -d $dir_name; then
		sleep 1
		datee=`date +h%Hm%Ms%S_m%md%d_%y`
		mkdir $dir_name
		echo $dir_name
	else
		mkdir $dir_name
		echo $dir_name
	fi

	mv $log $dir_name
	cd $dir_name
	:>$pid_log

fi

if test "$tmp" = "true"; then
	datee=`date +h%Hm%Ms%S_m%md%d_%y_%N`
	if test ! -d /tmp/$datee; then
		mkdir /tmp/$datee;
	fi
	tmp=/tmp/$datee;
	echo tmp file: $tmp.
fi


mnt=mnt.sh

function opt() {
    if test -n "$2"; then
        echo $1 $2
    fi
}

function default() {
    if test -z "$1"; then
        eval $1=$2
    fi
}
